//
//  MPStatusListViewModel.swift
//  MPSinaWeibo
//
//  Created by DaVinci on 16/4/13.
//  Copyright © 2016年 DaVinci. All rights reserved.
//

import UIKit
//import SVProgressHUD

class MPStatusListViewModel: NSObject {

    
    var statusArray = [MPStatusViewModel]()
    
    func loadHomeData(finished: (isSucceeded: Bool) -> ()){
    
    
        guard let token = MPUserAccountViewModel.sharedAccountViewModel.userAccount?.access_token else {
            print("用户暂未登录")
            return
        }
        
        HMNetworkTools.sharedTools.loadHomePageData(token) { (result, error) -> () in
            if error != nil {

                finished(isSucceeded: false)
                return
            }
            //网络请求成功
            print(result)
            //1.将result 转换为字典类型
            guard let dict = result as? [String : AnyObject] else {

                finished(isSucceeded: false)
                return
            }
            //2.根据 statuses key 获取对应的字典类型的数组
            guard let array = dict["statuses"] as? [[String : AnyObject]] else {

                finished(isSucceeded: false)
                return
            }
            //3.遍历数组
            //声明一个可变的数组
            var tempArray = [MPStatusViewModel]()
            for item in array {
                //4.字典转模型
                let s = MPStatus(dict: item)
                let viewModel = MPStatusViewModel(status: s)
                //5.将模型数据添加到数组中
                tempArray.append(viewModel)
            }
            //保存数组
            self.statusArray = tempArray
            
            finished(isSucceeded: true)
        }
    }

}

